#include "STC15F204E.h"

#define BAUD  0xFE80                    // 9600bps @ 11.0592MHz

sbit TXB = P3^1;                        //define UART TX port
sbit RXB = P3^0;                        //define UART RX port

typedef bit BOOL;
typedef unsigned char uchar;
typedef unsigned int uint;

uchar TBUF;
uchar TDAT;
uchar TCNT;
uchar TBIT;
BOOL TING;
BOOL TEND;

uchar Check = 0;//异或和校验码

sbit PLAY_STOP_KEY = P1^3;  // 播放/停止共用按键
sbit KEY3 = P1^4;           // 音量+键
sbit KEY4 = P1^2;           // 音量减键
sbit KEY5 = P1^1;           // 上一曲键
sbit KEY6 = P1^5;           // 下一曲键

// 播放状态标志
BOOL isPlaying = 0;

//指令码
uchar Play[] = {0x7E, 0x03, 0x01, 0x02, 0xEF};  // 播放指令
uchar Stop[] = {0x7E, 0x03, 0x0E, 0x0D, 0xEF};  // 停止指令
uchar Next[] = {0x7E, 0x03, 0x03, 0x00, 0xEF};  // 下一曲指令
uchar Last[] = {0x7E, 0x03, 0x04, 0x07, 0xEF};  // 上一曲指令
uchar Add[] = {0x7E, 0x03, 0x05, 0x06, 0xEF};   // 音量+指令
uchar Sub[] = {0x7E, 0x03, 0x06, 0x05, 0xEF};   // 音量-指令

void Uart_Init(void);                          // 串口初始化
void Uart_Send_Char(uchar dat);                // 发送单个字节数据
uchar BCCheck(uchar *nDataBuf, uchar nLen);    // 异或检验函数
void Delay(uint xms);                          // 延时函数
void Uart_Send_String(uchar *p, uchar cnt);    // 发送字符串或者数组

void main()
{
    Uart_Init();
    Delay(100);
    
    //Uart_Send_String("System Started\r\n", sizeof("System Started\r\n"));
    
    while (1)
    {
        P17 = 0;
        Delay(500);
        P17 = 1;
        Delay(500);
        
        // 播放/停止按键处理
        if(PLAY_STOP_KEY == 0)
        {
            while(!PLAY_STOP_KEY);  // 等待按键松开
            
            if(isPlaying)
            {
                // 当前正在播放，发送停止指令
                Uart_Send_String(Stop, sizeof(Stop));
                isPlaying = 0;  // 更新状态为停止
            }
            else
            {
                // 当前已停止，发送播放指令
                Play[0] = 0;
                Check = BCCheck(Play, 3);
                Play[0] = 0x7E;
                Play[3] = Check;
                Uart_Send_String(Play, sizeof(Play));
                isPlaying = 1;  // 更新状态为播放
            }
        }
        else if(KEY3 == 0)      // 音量+
        {
            while(!KEY3);
            Uart_Send_String(Add, sizeof(Add));
        }
        else if(KEY4 == 0)      // 音量-
        {
            while(!KEY4);
            Uart_Send_String(Sub, sizeof(Sub));
        }
        else if(KEY5 == 0)      // 上一曲
        {
            while(!KEY5);
            Uart_Send_String(Next, sizeof(Next));
        }
        else if(KEY6 == 0)      // 下一曲
        {
            while(!KEY6);
            Uart_Send_String(Last, sizeof(Last));
        }
    }
}


//timer0中断回调函数
void tm0() interrupt 1
{
    if (--TCNT == 0)
    {
        TCNT = 3;                       //reset send baudrate counter
        if (TING)                       //judge whether sending
        {
            if (TBIT == 0)
            {
                TXB = 0;                //send start bit
                TDAT = TBUF;            //load data from TBUF to TDAT
                TBIT = 9;               //initial send bit number (8 data bits + 1 stop bit)
            }
            else
            {
                TDAT >>= 1;             //shift data to CY
                if (--TBIT == 0)
                {
                    TXB = 1;
                    TING = 0;           //stop send
                    TEND = 1;           //set send completed flag
                }
                else
                {
                    TXB = CY;           //write CY to TX port
                }
            }
        }
    }
}


//初始化串口
void Uart_Init(void)
{
    TMOD = 0x00;                        //timer0 in 16-bit auto reload mode
    AUXR = 0x80;                        //timer0 working at 1T mode
    TL0 = BAUD;
    TH0 = BAUD>>8;                      //initial timer0 and set reload value
    TR0 = 1;                            //tiemr0 start running
    ET0 = 1;                            //enable timer0 interrupt
    PT0 = 1;                            //improve timer0 interrupt priority
    EA = 1;                             //open global interrupt switch

    TING = 0;
    TEND = 1;
    TCNT = 0;
}


//发送单个字节数据函数
void UART_Send_Char(uchar dat)
{
    while (!TEND);      // 等待上一次发送完成
    TEND = 0;           // 清除发送完成标志
    TBUF = dat;         // 加载要发送的数据
    TING = 1;           // 启动发送
}



//异或检验函数
uchar BCCheck(uchar *nDataBuf, uchar nLen)
{
	uchar nSum = 0;
	uchar i = 0;
	for(i=0; i<nLen; i++)
	{
		nSum ^= nDataBuf[i];
    }
    return nSum;
}

//延时函数
void Delay(uint xms)	//@11.0592MHz
{
    uint i, j;
    for(i = xms; i > 0; i--)
        for(j = 114; j > 0; j--);
}


//发送字符串或者数组
void UART_Send_String(uchar *p,uchar cnt)
{
    while(cnt>0)
    {
        Uart_Send_Char(*p++);
        Delay(5);
        cnt--;
    }
}